<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Blockly Demo: Mirrored Blockly</title>
    <script src="./node_modules/blockly/blockly_compressed.js"></script>
    <script src="./node_modules/blockly/blocks_compressed.js"></script>
    <script src="./node_modules/blockly/msg/en.js"></script>
    <style>
      body {
        background-color: #fff;
        font-family: sans-serif;
      }
      h1 {
        font-weight: normal;
        font-size: 140%;
      }
    </style>
  </head>
  <body>
    <p>
      This is a simple demo of a primary Blockly instance that controls a
      secondary Blockly instance with events. Open the JavaScript console to see
      the event passing.
    </p>

    <p>
      &rarr; More info on
      <a
        href="https://developers.google.com/blockly/guides/configure/web/events"
        >events</a
      >&hellip;
    </p>

    <table width="100%">
      <tr>
        <td>
          <div id="primaryDiv" style="height: 480px; width: 600px"></div>
        </td>
        <td>
          <div id="secondaryDiv" style="height: 480px; width: 430px"></div>
        </td>
      </tr>
    </table>

    <script>
      var toolbox = {
        kind: 'flyoutToolbox',
        contents: [
          {
            kind: 'block',
            type: 'controls_if',
          },
          {
            kind: 'block',
            type: 'logic_compare',
          },
          {
            kind: 'block',
            type: 'controls_repeat_ext',
          },
          {
            kind: 'block',
            type: 'math_number',
            fields: {
              NUM: 123,
            },
          },
          {
            kind: 'block',
            type: 'math_arithmetic',
          },
          {
            kind: 'block',
            type: 'text',
          },
          {
            kind: 'block',
            type: 'text_print',
          },
          {
            kind: 'block',
            type: 'variables_get',
            fields: {
              VAR: {
                name: 'i',
              },
            },
          },
          {
            kind: 'block',
            type: 'variables_get',
            fields: {
              VAR: {
                name: 'j',
              },
            },
          },
          {
            kind: 'block',
            type: 'variables_get',
            fields: {
              VAR: {
                name: 'k',
              },
            },
          },
        ],
      };

      // Inject primary workspace.
      var primaryWorkspace = Blockly.inject('primaryDiv', {
        media: './node_modules/blockly/media/',
        toolbox: toolbox,
      });
      // Inject secondary workspace.
      var secondaryWorkspace = Blockly.inject('secondaryDiv', {
        media: './node_modules/blockly/media/',
        readOnly: true,
      });
      // Listen to events on primary workspace.
      primaryWorkspace.addChangeListener(mirrorEvent);

      function mirrorEvent(primaryEvent) {
        if (primaryEvent.isUiEvent) {
          return; // Don't mirror UI events.
        }
        // Convert event to JSON.  This could then be transmitted across the net.
        var json = primaryEvent.toJson();
        console.log(json);
        // Convert JSON back into an event, then execute it.
        var secondaryEvent = Blockly.Events.fromJson(json, secondaryWorkspace);
        secondaryEvent.run(true);
      }
    </script>
  </body>
</html>
